package purchase.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.JbpmContext;

import purchase.domain.Purchase;
import purchase.service.PurchaseService;
import purchase.service.impl.PurchaseServiceImpl;

public class PurchaseActionServletHandler {
	
	private Log log = LogFactory.getLog(PurchaseActionServletHandler.class);
	
	private HttpServletRequest request;
	
	private HttpServletResponse response;
	
	private JbpmContext jbpmContext;
	
	private PurchaseService purchaseService;

	public PurchaseActionServletHandler(HttpServletRequest request,
			HttpServletResponse response, JbpmContext jbpmContext) {
		super();
		this.request = request;
		this.response = response;
		this.jbpmContext = jbpmContext;
		
		purchaseService = new PurchaseServiceImpl(jbpmContext);
	}
	
	/**
	 * set message
	 * @param meg
	 */
	private void setMessage(String meg) {
		request.setAttribute("message", meg);
	}

	private void getPurchaseTask() {
		Long purchaseId = Long.parseLong(request.getParameter("purchaseId"));
		Long taskId = Long.parseLong(request.getParameter("taskId"));
		
		Purchase purchase = purchaseService.findPurchaseAndProcessById(purchaseId, taskId);
		request.setAttribute("purchase", purchase);
	}

	/**
	 * login
	 * @throws ServletException
	 * @throws IOException
	 */
	public void login() throws ServletException, IOException {
		String username = request.getParameter("username");
		// String password = request.getParameter("password");

		log.info(username + " login system.");

		// 用户存入session
		request.getSession().setAttribute("user", username);
		
		// 跳转
		request.getRequestDispatcher("/purchase/main.jsp").forward(request, response);
	}

	/**
	 * forward add page
	 * @throws ServletException
	 * @throws IOException
	 */
	public void inputPurchase() throws ServletException, IOException {
		request.getRequestDispatcher("/purchase/add.jsp").forward(request, response);
	}
	
	public void createPurchase() throws ServletException, IOException {
		
		String title = request.getParameter("title");
		String remark = request.getParameter("remark");
		String manager = request.getParameter("manager");
		String issuePerson = (String)request.getSession().getAttribute("user");
		
		Purchase purchase = new Purchase();
		purchase.setTitle(title);
		purchase.setRemark(remark);
		purchase.getTaskBean().setProcessInstanceCreater(issuePerson);
		purchase.getTaskBean().setManager(manager);
		
		purchaseService.createPurchaseAndStartProcess(purchase);
		setMessage("create purchase success!");
		
		request.getRequestDispatcher("/purchase/message.jsp").forward(request, response);
	}
	
	public void myTaskList() throws ServletException, IOException {
		String currentLoginUser = (String)request.getSession().getAttribute("user");
		
		List<Purchase> purchaseTaskList = purchaseService.getMyTaskList(currentLoginUser);
		request.setAttribute("purchaseTaskList", purchaseTaskList);
		
		request.getRequestDispatcher("/purchase/task_list.jsp").forward(request, response);
	}

	public void modifyPurchase() throws ServletException, IOException {

		Long id = Long.parseLong(request.getParameter("purchaseId"));
		String title = request.getParameter("title");
		String remark = request.getParameter("remark");
		Long taskInstanceId = Long.parseLong(request.getParameter("taskInstanceId"));
		
		Purchase purchase = new Purchase();
		purchase.setId(id);
		purchase.setTitle(title);
		purchase.setRemark(remark);
		purchase.getTaskBean().setTaskInstanceId(taskInstanceId);
		
		purchaseService.modifyPurchaseAndProcess(purchase);
		
		setMessage("modify purchase success!");
		
		request.getRequestDispatcher("/purchase/message.jsp").forward(request, response);
	}

	public void inputModifyPurchase() throws ServletException, IOException {
	
		getPurchaseTask();
		
		request.getRequestDispatcher("/purchase/modifyPurchase.jsp").forward(request, response);
	}

	public void cancelPurchase() throws ServletException, IOException {
		
		Long id = Long.parseLong(request.getParameter("purchaseId"));
		Long taskInstanceId = Long.parseLong(request.getParameter("taskInstanceId"));
		
		Purchase purchase = new Purchase();
		purchase.setId(id);
		purchase.getTaskBean().setTaskInstanceId(taskInstanceId);
		
		purchaseService.cancelPurchaseAndProcess(purchase);
		
		setMessage("cancel purchase success!");
		
		request.getRequestDispatcher("/purchase/message.jsp").forward(request, response);
	}

	public void inputCancelPurchase() throws ServletException, IOException {
		
		getPurchaseTask();
		
		request.getRequestDispatcher("/purchase/cancelPurchase.jsp").forward(request, response);
	}

	public void mgrApprovePurchase() throws ServletException, IOException {

		Long id = Long.parseLong(request.getParameter("purchaseId"));
		Long taskInstanceId = Long.parseLong(request.getParameter("taskInstanceId"));
		String mgrAdvice = (String)request.getParameter("mgrAdvice");
		
		Purchase purchase = new Purchase();
		purchase.setId(id);
		purchase.getTaskBean().setTaskInstanceId(taskInstanceId);
		purchase.getTaskBean().setManagerAdvice(mgrAdvice);
		
		
		purchaseService.mgrApprovePurchaseAndProcess(purchase);
		
		setMessage("manager approve success!");
		
		request.getRequestDispatcher("/purchase/message.jsp").forward(request, response);
	}

	public void inputMgrApprovePurchase() throws ServletException, IOException {
		
		getPurchaseTask();
		
		request.getRequestDispatcher("/purchase/mgrApprovePurchase.jsp").forward(request, response);
	}
	
}
